package cn.wolfcode.rbac.controller;


import cn.wolfcode.rbac.annotation.RequiredPermission;
import cn.wolfcode.rbac.config.PageInfo;
import cn.wolfcode.rbac.config.QueryObject;
import cn.wolfcode.rbac.model.DepartmentDO;
import cn.wolfcode.rbac.request.DepartmentRequest;
import cn.wolfcode.rbac.service.DepartmentService;
import cn.wolfcode.rbac.util.JsonData;
import cn.wolfcode.rbac.vo.DepartmentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lu.F
 * @since 2022-05-14
 */
//@RestController
//@RequestMapping("/api/department/v1")
@Controller
@RequestMapping("/department")
@Api(tags = "部门模块")
public class DepartmentController {


    @Autowired
    private DepartmentService departmentService;


    /**
     * 查询所有部门信息
     * @return
     */
    @RequestMapping("list")
    @RequiredPermission({"部门列表","department:list"})
    public String selectAll(Model model, QueryObject queryObject){
//        List<DepartmentDO> departmentDOList = departmentService.selectAll();
//        model.addAttribute("departments",departmentDOList);
        PageInfo<DepartmentVO> pageInfo = departmentService.pageDepartment(queryObject.getCurrentPage(), queryObject.getPageSize());
        model.addAttribute("pageInfo",pageInfo);
        return "department/list";
    }

    /**
     * 部门编辑页面请求   添加、修改
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/input")
    @RequiredPermission({"部门编辑","department:input"})
    public String input(Model model,Long id){

        //如果id不为null,表示是修改操作操作，需要回显数据到页面，为空则表示添加，不需要回显数据
        if(id != null){
            //根据id获取要修改的部门对象
            DepartmentDO department = departmentService.selectByPrimaryKey(id);
            //将要修改的部门对象存储到域对象中
            model.addAttribute("department",department);
        }
        //跳转到部门编辑页面
        return "department/input";
    }


    /**
     * 部门编辑请求   添加、修改
     * @param departmentRequest
     * @return
     */
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"部门保存或更新","department:saveOrUpdate"})
    public String saveOrUpdate(DepartmentRequest departmentRequest){
        //如果参数部门对象id为空表示添加，不为空表示修改
        departmentService.saveOrUpdate(departmentRequest);
        //跳转回部门列表页面请求，展示最新数据
        return "redirect:/department/list";
    }

    //部门删除请求
    @RequestMapping("/delete")
    @RequiredPermission({"部门删除","department:delete"})
    public String delete(Long id){
        //根据id删除
        departmentService.deleteByPrimaryKey(id);
        //跳转回部门列表页面请求，展示最新数据
        return "redirect:/department/list";
    }

/*
    *//**
     * 根据主键查询部门信息
     * @param id
     * @return
     *//*
    @ApiOperation("根据主键查询部门信息")
    @GetMapping("find/{id}")
    public JsonData selectByPrimaryKey(@ApiParam("部门id") @PathVariable("id")Long id){

        DepartmentDO departmentDO = departmentService.selectByPrimaryKey(id);

        return departmentDO != null ? JsonData.buildSuccess(departmentDO):JsonData.buildError("对象不存在");
    }


    *//**
     * 添加部门信息
     * @param departmentRequest
     * @return
     *//*
    @ApiOperation("添加部门")
    @PostMapping("save")
    public JsonData save(@ApiParam("部门请求体对象")@RequestBody DepartmentRequest departmentRequest){
        int row = departmentService.insert(departmentRequest);
        return row == 1 ? JsonData.buildSuccess():JsonData.buildError("新增失败，请重试");
    }


    *//**
     * 修改部门信息
     * @param departmentRequest
     * @return
     *//*
    @ApiOperation("修改部门信息")
    @PostMapping("update")
    public JsonData updateByPrimaryKey(@ApiParam("部门请求体对象")@RequestBody DepartmentRequest departmentRequest){
        int row = departmentService.updateByPrimaryKey(departmentRequest);
        return row == 1 ? JsonData.buildSuccess():JsonData.buildError("修改失败，请重试");
    }

    *//**
     * 根据主键删除部门信息
     * @param id
     * @return
     *//*
    @ApiOperation("根据主键删除部门信息")
    @GetMapping("delete/{id}")
    public JsonData deleteByPrimaryKey(@ApiParam("部门id")@PathVariable("id") long id){
        int row = departmentService.deleteByPrimaryKey(id);
        return row == 1 ? JsonData.buildSuccess():JsonData.buildError("删除失败，请重试");
    }


    *//**
     * 部门分页查找
     * @param page
     * @param size
     * @return
     *//*
    @ApiOperation("部门分页查找")
    @GetMapping("page")
    public JsonData pageDepartment(@ApiParam("当前页")@RequestParam("page") int page,@ApiParam("每页显示条数") @RequestParam("size") int size){
        return JsonData.buildSuccess(departmentService.pageDepartment(page,size));
    }*/

}

